import {
    Controller, Post, UseInterceptors, UploadedFile, UploadedFiles,
    ParseFilePipe,
    FileTypeValidator,
    MaxFileSizeValidator
} from '@nestjs/common';
import {FileInterceptor, FilesInterceptor} from '@nestjs/platform-express'
import {Express} from 'express'
import {FileSizeValidationPipe} from './file-size-validation.pipe'

@Controller('app')
export class AppController {
    // 上传单文件 FileInterceptor UploadedFile
    @Post('upload')
    @UseInterceptors(FileInterceptor('file'))
    uploadFile(@UploadedFile(FileSizeValidationPipe) file: Express.Multer.File) {
        console.log(file);
        return 'file 上传成功';
    }

    @Post('upload-validator')
    @UseInterceptors(FileInterceptor('file'))
    uploadFileValidator(@UploadedFile(new ParseFilePipe({
        validators: [
            new FileTypeValidator({fileType: 'image/png'}),
            new MaxFileSizeValidator({maxSize: 1024 * 50}),
        ]
    })) file: Express.Multer.File) {
        console.log(file);
        return 'file 上传成功';
    }

    // // 上传多个文件 FilesInterceptor UploadedFiles
    // @Post('uploads')
    // @UseInterceptors(FilesInterceptor('files', 10))
    // uploadFiles(@UploadedFiles() files: Array<Express.Multer.File>) {
    //     console.log(files);
    //     return 'files 上传成功';
    // }
}
